package com.woniu.backstage.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.backstage.common.PageResult;
import com.woniu.backstage.dao.TbUserMapper;
import com.woniu.backstage.entity.TbUser;
import com.woniu.backstage.entity.form.UserForm;
import com.woniu.backstage.entity.form.VipsForm;
import com.woniu.backstage.service.ITbUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.List;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author spirit
 * @since 2020-04-15
 */
@Service
public class TbUserServiceImpl extends ServiceImpl<TbUserMapper, TbUser> implements ITbUserService {

    @Autowired
    private TbUserMapper userMapper;

    @Override
    public PageResult getAllUser(Integer currentPage, Integer pageSize) {
        //查询出总的数据条数
        Integer total = userMapper.getTotal();
        //计算出开始的数据
        int begin = (currentPage-1)*pageSize;
        //查询出数据
        List<TbUser> userList = userMapper.getAllUser(begin, pageSize);
        PageResult pageResult = new PageResult(0,"success",userList,currentPage,total);
        return pageResult;
    }

    @Override
    public int addUser(TbUser user) {
        int result = userMapper.insert(user);
        return result;
    }

    @Override
    public int deleteUser(Serializable id) {
        return userMapper.deleteById(id);
    }

    @Override
    public int updateUser(TbUser user) {
        return userMapper.updateUser(user);
    }

    @Override
    public Integer deleteBatch(List<Integer> idArr) {
        return userMapper.deleteBatch(idArr);
    }

    @Override
    public IPage<TbUser> pageLimit(Page<TbUser> page, UserForm userForm) {
        QueryWrapper<TbUser> wrapper = new QueryWrapper<>();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        if(userForm.getSex() != null){
            wrapper.eq("sex",userForm.getSex());
        }
        if(userForm.getLevel() != null){
            wrapper.eq("level",userForm.getLevel());
        }
        if(userForm.getStartTime() != null){
            wrapper.ge("create_time",simpleDateFormat.format(userForm.getStartTime()));
        }
        if(userForm.getEndTime() != null){
            wrapper.le("create_time",simpleDateFormat.format(userForm.getEndTime()));
        }
        IPage<TbUser> userIPage = userMapper.selectPage(page,wrapper);
        System.out.println("userIPage:"+userIPage);
        return userIPage;
    }

    @Override
    public IPage<TbUser> vipsShow(Page<TbUser> page, VipsForm vipsForm) {
        QueryWrapper<TbUser> wrapper = new QueryWrapper<>();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        if(vipsForm.getPhone() != null && vipsForm.getPhone().length() > 0){
            wrapper.eq("phone",vipsForm.getPhone());
        }
        if(vipsForm.getStartTime() != null){
            wrapper.ge("vip_start_time",simpleDateFormat.format(vipsForm.getStartTime()));
        }
        if(vipsForm.getEndTime() != null){
            wrapper.le("vip_start_time",simpleDateFormat.format(vipsForm.getEndTime()));
        }
        wrapper.eq("level",1);
        IPage<TbUser> vipsIPage = userMapper.selectPage(page, wrapper);
        return vipsIPage;
    }

    @Override
    public TbUser selectById(Serializable id) {
        TbUser user = userMapper.selectById(id);
        return user;
    }

    @Override
    public List<TbUser> selectAll() {
        return userMapper.selectAll();
    }
}
